#include "algostuff.h"
using namespace std;

void find1()
{
	list<int> coll;

	INSERT_ELEMENTS(coll, 1, 9);
	INSERT_ELEMENTS(coll, 1, 9);
	PRINT_ELEMENTS(coll, "coll: ");

	// find first element with value 4
	list<int>::iterator pos1;
	pos1 = find(coll.begin(), coll.end(), 4);

	// find second element with value 4
	list<int>::iterator pos2;
	if (pos1 != coll.end())
	{
		pos2 = find(++pos1, coll.end(), 4);
	}

	// print all elements from first to second 4(both included)
	if (pos1 != coll.end() && pos2 != coll.end())
	{
		copy(--pos1, ++pos2, ostream_iterator<int>(cout, " "));
		cout << endl;
	}
}

// use find_if() and find_if_not()
void find2()
{
	vector<int> coll;
	vector<int>::iterator pos;

	INSERT_ELEMENTS(coll, 1, 9);
	PRINT_ELEMENTS(coll, "coll: ");

	// find first element greater than 3
	pos = find_if(coll.begin(), coll.end(), bind(greater<int>(), std::placeholders::_1, 3));

	// print its position
	cout << "the " << distance(coll.begin(), pos) + 1 << ". element is the first greater than 3" << endl;

	// find first element divisible by 3
	pos = find_if(coll.begin(), coll.end(), [](int elem){return elem % 3 == 0; });

	// print its position
	cout << "the " << distance(coll.begin(), pos) + 1 << ". element is the first divisible by 3" << endl;

	// find first element not < 5
	pos = find_if_not(coll.begin(), coll.end(), bind(less<int>(), std::placeholders::_1, 5));
	cout << "first value >= 5: " << *pos << endl;
}